package com.example.eazytransfer.ui;

import com.example.eazytransfer.Main;
import com.example.eazytransfer.model.Config;
import com.example.eazytransfer.model.TCPUtil;
import com.example.eazytransfer.model.ViewModel;
import com.google.gson.Gson;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class HelloApplication extends Application {

    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        stage.setTitle("EasyTransfer");
        stage.getIcons().add(new Image("image/transfer.png"));
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
        readConfig();

    }

    private void readConfig() throws IOException {
        String json=FileUtils.readFileToString(new File(Main.configFile),"UTF-8");
        Config config=new Gson().fromJson(json, Config.class);
        ViewModel.getInstance().dirForReceive.set(config.getReceiveFileDir());
    }

    public static void main(String[] args) {
        launch();
    }

    @Override
    public void stop() throws Exception {
        TCPUtil.getInstance().close();
        super.stop();
    }
}